package com.chenglu.android.mine.coupon.mvp.presenter;


import android.accounts.NetworkErrorException;

import com.chenglu.android.common.base.BasePresenter;
import com.chenglu.android.common.net.Constant;
import com.chenglu.android.common.net.RxScheduler;
import com.chenglu.android.common.net.exception.NetErrorException;
import com.chenglu.android.common.utils.LogUtils;
import com.chenglu.android.common.utils.ToastUtils;
import com.chenglu.android.mine.bean.CouponBean;
import com.chenglu.android.mine.coupon.mvp.contract.CouponContract;
import com.chenglu.android.mine.coupon.mvp.model.CouponModel;
import com.chenglu.android.payment.bean.CouponListBean;

import java.net.ConnectException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.TimeoutException;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @author: lyj
 * @Created by Copyright 2023/3/2 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 */
public class CouponPresenter extends BasePresenter<CouponContract.View> implements CouponContract.CouponPresenter {

    private CouponContract.CouponModel model;

    public CouponPresenter() {
        model = new CouponModel();
    }


    @Override
    public void getCouponList(String token, Map<String,String> map) {
        if(!isViewAttached()){
            return;
        }
        model.getCouponList(token,map)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new Observer<CouponBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mView.showLoading();
                    }

                    @Override
                    public void onNext(@NonNull CouponBean bean) {
                        mView.dismissLoading();
                        if(bean.getCode()==(Constant.HTTPCODE200)){
                            mView.getCouponListSuccess(bean);
                        }else if(bean.getCode()==(Constant.HTTPCODE400)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE401)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE404)) {
                            ToastUtils.show(bean.getMsg());
                        }else if(bean.getCode()==(Constant.HTTPCODE500)) {
                            ToastUtils.show(bean.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mView.dismissLoading();
                        NetErrorException.apiError(e,mView);

                        LogUtils.e("qqqqqqq:"+"onError:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.dismissLoading();
                    }
                });
    }
}
